# -*- coding: utf-8 -*-
#
# w kazdej funkcji w tym pliku listy postingowe sa przedstawione jako czworki liczb calkowitych
# ( artykuly, ilosc artykulow, współczynniki, pochodzenie - 1 z indeksu, 0 wtorne  )
#  ^^ int *  ^    int         ^ float *     ^      int                             ^
#

import rapidUtils
import createIndex
import stemming as c_stemming
import normalizeQuery

def andArticles(l1, l2):
    answer = []
    i = 0
    j = 0
    while True:
        if i >= len(l1):
            return answer 
        elif j >= len(l2):
            return answer
        elif l1[i] == l2[j]:
            answer.append( l1[i] )
            i += 1
            j += 1
        elif l1[i] > l2[j]:
            j += 1
        else:
            i += 1

def orArticles(l1,l2):
    if( len(lisA) == 0 or len(lisB) == 0):
        return ([],[])
    answer = []
    i = 0
    j = 0
    while True:
        if i >= len(l1):
            return answer + l2[j:] 
        elif j >= len(l2):
            return answer + l1[i:]
        elif l1[i] == l2[j]:
            answer.append( l1[i] )
            i += 1
            j += 1
        elif l1[i] > l2[j]:
            if len(answer) == 0 or answer[-1] != l2[j]:
                answer.append( l2[j] )
            j += 1
        else:
            if len(answer) == 0 or  answer[-1] != l1[i]:
                answer.append( l1[i] )            
            i += 1


def orListPositions(l):
    i = 0
    answer = (0, 0, 0, 0, 1)
    for elem in l:
        if i == 0:
            i = 1
            answer = elem
            continue
        answer = rapidUtils.orPositions(answer ,elem)
    return answer
    
def andListPositions(l):
    i = 0
    answer = (0, 0, 0, 0, 1)
    for elem in l:
        if i == 0:
            i = 1
            answer = elem
            continue
        answer = rapidUtils.andPositions(answer ,elem, i)
        i += 1
    return answer
    
def orListArticles(l):
    i = 0
    answer = (0, 0, 0, 0, 1)
    for elem in l:
        if i == 0:
            i = 1
            answer = elem
            continue
        answer = rapidUtils.orArticles(answer ,elem)
    return answer
        
def andListArticles(l):
    i = 0
    answer = (0, 0, 0, 0, 1)
    for elem in l:
        if i == 0:
            i = 1
            answer = elem
            continue
        answer = rapidUtils.andArticles(answer ,elem)
    return answer


stemming = False
morfBases = []


# zwraca numery z noda dla slowa
def getNumbersForWord(word):
    answer = []
    number = createIndex.getVal(word) - 1
    # 804091 to ilosc slow w morfo
    if number < 804091 and number > 0:
        if len( morfBases[number] ) > 0 :
            return morfBases[number]

    sToken = c_stemming.stemmToken(word) 
 
    
    i = -1  
    if sToken is not None and stemming:
        i = createIndex.getVal(sToken)
    
    j = createIndex.getVal(word)
    
    answer = []
    
    if i > 0:
        answer.append(i)
    if j > 0:
        answer.append(j)
    
    return answer

# zwraca z indeksu liste 5i
def getListForWord(word):
    answer = []
    for elem in getNumbersForWord(word):
        answer.append( createIndex.get(elem) )
        #assert rapidUtils.getArticles( createIndex.get(elem) ) == createIndex.getArticles(elem)
    return orListPositions( answer )

# wartosc zwracana to 5i
def queryPositions(napis):
    napis = normalizeQuery.normalize(napis)
    andQueries = napis.split(" ") 
    answerAnd = []
    for andQuery in andQueries:
        split = andQuery.split("|")
        answerAnd.append( orListPositions( [ getListForWord(elem) for elem in split ] ) )

    answer = andListPositions( answerAnd )

    return answer


# wartosc zwracana to 5i
def queryArticles(napis):
    napis = normalizeQuery.normalize(napis)
    andQueries = napis.split(" ")
    answerAnd = []
    for andQuery in andQueries:
        split = andQuery.split("|")
        answerAnd.append( orListArticles ( [ getListForWord(elem) for elem in split ] ) )

    return andListArticles ( answerAnd )
